package com.aop;

import com.controller.base.BaseController;
import com.controller.base.Response;
import com.model.BackUser;
import com.model.FrontUser;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class ControllerAop {

    @Pointcut(value = "@annotation(com.anno.BackLogin)")
    public void BackLoginAuthPoint() { }

    @Pointcut(value = "@annotation(com.anno.FrontLogin)")
    public void FrontLoginAuthPoint() { }

    @Around("BackLoginAuthPoint()")
    public Object BackLoginAuthCheck(ProceedingJoinPoint joinPoint) throws Throwable {
        // 检查是否已经登录
        HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        BackUser user = (BackUser) request.getSession().getAttribute("user");
        if(user == null) return new Response().setCode(BaseController.DENIED).setMsg("权限不足,拒绝访问");
        return joinPoint.proceed();
    }

    @Around("FrontLoginAuthPoint()")
    public Object FrontLoginAuthCheck(ProceedingJoinPoint joinPoint) throws Throwable {
        // 检查是否已经登录
        HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        FrontUser user = (FrontUser) request.getSession().getAttribute("front_user");
        if(user == null) return new Response().setCode(BaseController.DENIED).setMsg("未登陆, 请登陆用户!");
        return joinPoint.proceed();
    }




}
